package com.txf.simple.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.txf.simple.R;
import com.txf.simple.tools.DelayedTaskTools;
import com.txf.simple.tools.Logger;
import com.txf.simple.utils.UIUtils;

/**
 * @author txf
 * @Title
 * @package com.txf.simple.ui.view
 * @date 2016/11/25 0025
 */

public class LoadingCustomView extends View {
    private Paint mPaint;//画笔
    private float mRadius;//圆半径
    private RectF mRectF;//圆矩形范围
    float mStartAngle = 0;//圆弧的开始角度 0度== 3点钟方向
    float mEndAngle = 0;//圆弧结束角度
    private float mStrokeWidth;//笔画宽度

    public LoadingCustomView(Context context) {
        super(context);
        init();
    }

    public LoadingCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadingCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        mStrokeWidth = UIUtils.dp2px(5);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initRectf(w, h);
    }

    /**
     * 初始化圆的矩形 圆的半径
     */
    private void initRectf(int w, int h) {
        float rx = w / 2;
        float ry = h / 2;
        mRadius = Math.min(rx, ry);
        float left, top, right, bottom;
        int wh = (int) (Math.min(w, h) - mStrokeWidth * 2);
        left = 0;
        top = 0;
        right = wh;
        bottom = wh;
        if (w - wh > 0) {
            left = (w - wh) / 2;
            right = left + wh;
        }
        if (h - wh > 0) {
            top = (h - wh) / 2;
            bottom = top + wh;
        }
        mRectF = new RectF(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //底层圆
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_ffffff));
        canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
        //不断绘制的圆弧
        if (Math.abs(mEndAngle) >= 1) {
            mPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
//            canvas.drawArc(mRectF, mStartAngle, mEndAngle, true, mPaint);
            canvas.drawArc(mRectF, mStartAngle, mEndAngle, false, mPaint);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        // 结束延时任务
        if (taskTools != null) {
            taskTools.setOnDelayedTaskListener(null);
            taskTools.stopForwardTimer();
        }
    }

    /**
     * @param startAngle 圆弧开始角度
     * @param endAngle   圆弧结束角度
     */
    private void setdrawArc(float startAngle, float endAngle) {
        this.mStartAngle = startAngle;
        this.mEndAngle = endAngle;
        invalidate();
    }

    private DelayedTaskTools taskTools;//延时任务
    private float maxAngle = 270;
    /**
     * 开始延迟任务画圆弧
     */
    public void startTask() {
        if (taskTools != null) {
            taskTools.startForwardTimer(1, 0);
        } else {
            taskTools = new DelayedTaskTools(new DelayedTaskTools.OnDelayedTaskListener() {
                @Override
                public void onHandleMessage(int msg) {
                    Logger.i("开始角度 : " + mStartAngle + " , 结束角度 : " + mEndAngle);
                    setdrawArc(mStartAngle, mEndAngle);
                    if (mEndAngle >= maxAngle) {
                        if (mStartAngle + maxAngle >= 360) {
                            mStartAngle = mStartAngle + maxAngle - 360;
                        } else
                            mStartAngle = maxAngle;
                    }
                    if (mEndAngle >= maxAngle){
                        mEndAngle = -maxAngle;
                    }
                    mEndAngle = mEndAngle+7.2f;
                    taskTools.startForwardTimer(1, 0);
                }
            });
            taskTools.startForwardTimer(1, 0);
        }
    }
}
